package com.hxl.demo.controller;

import com.hxl.demo.entity.Post;
import com.hxl.demo.service.Impl.PostServiceImpl;
import com.hxl.demo.tools.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")

public class PostController {

    @Autowired
    private PostServiceImpl postService;

    @Autowired
    Post post;

    /**
     * 获取岗位信息
     * @param limit
     * @param page
     * @param id
     * @return
     */
    @RequestMapping("/getPost")
    public Object getPost(@RequestParam(name = "limit",required = false) Integer limit,
                          @RequestParam(name = "page",required = false) Integer page,
                          @RequestParam(name = "id",required = false) Integer id,
                          @RequestParam(name = "postName",required = false) String postName,
                          @RequestParam(name = "pinName",required = false) String pinName,
                          @RequestParam(name = "deptName",required = false) String deptName){


        List<Post> list=postService.Sel(id,limit,page,postName,pinName,deptName);
        Integer num=postService.SelNum(postName,pinName,deptName);

//        Map res= new HashMap<>();
//        res.put("data",list);
//        res.put("page",page);
//        res.put("limit",limit);

        return  Common.returnJsonList("操作成功",200,list,num);
    }
    @RequestMapping("/postAdd")
    public Object postAdd(@RequestParam(name = "postName") String postName,@RequestParam(name = "pinName") String pinName){
        post.setPostName(postName);
        post.setPinName(pinName);
        post.setInsertTime(new Date());
        Integer id= postService.postAdd(post);

        if(id!=null){
            return Common.returnJsonFail("操作成功",200);
        }
        return Common.returnJsonFail("操作失败",400);
    }
    @RequestMapping("/postUpdate")
    public Object postUpdate(@RequestParam(name = "postName") String postName,@RequestParam(name = "pinName") String pinName,@RequestParam(name = "id") Integer id){
        if(postName.length()<2 || postName.length()>30){
               return Common.returnJsonFail("岗位名称长度须在2-30字符之间",400);
        }
        if(pinName.length()<2 || pinName.length()>30){
            return Common.returnJsonFail("岗位拼音首字母长度须在2-30字符之间",400);
        }


        return postService.postUpdate(id,postName,pinName);
    }
    @RequestMapping("/postDelete")
    public Object postUpdate(@RequestParam(name = "id") Integer id){


        return postService.postDelete(id);
    }

}
